package org.dromara.system.controller.system;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.CommodityExchangeBo;
import org.dromara.system.domain.bo.PointsMallRecordsBo;
import org.dromara.system.domain.vo.PointsMallRecordsVo;
import org.dromara.system.service.IPointsMallRecordsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 积分商城订单
 *
 * @author Lion Li
 * @date 2025-07-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/mallRecords")
public class PointsMallRecordsController extends BaseController {

    private final IPointsMallRecordsService pointsMallRecordsService;

    /**
     * 查询积分商城订单列表
     */
//    @SaCheckPermission("system:mallRecords:list")
    @GetMapping("/list")
    public TableDataInfo<PointsMallRecordsVo> list(PointsMallRecordsBo bo, PageQuery pageQuery) {
        return pointsMallRecordsService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/listReception")
    public TableDataInfo<PointsMallRecordsVo> listReception(PointsMallRecordsBo bo, PageQuery pageQuery) {
        return pointsMallRecordsService.listReception(bo, pageQuery);
    }

    /**
     * 导出积分商城订单列表
     */
//    @SaCheckPermission("system:mallRecords:export")
    @Log(title = "积分商城订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PointsMallRecordsBo bo, HttpServletResponse response) {
        List<PointsMallRecordsVo> list = pointsMallRecordsService.queryList(bo);
        ExcelUtil.exportExcel(list, "积分商城订单", PointsMallRecordsVo.class, response);
    }

    /**
     * 获取积分商城订单详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:mallRecords:query")
    @GetMapping("/{id}")
    public R<PointsMallRecordsVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable Long id) {
        return R.ok(pointsMallRecordsService.queryById(id));
    }

    /**
     * 新增积分商城订单
     */
//    @SaCheckPermission("system:mallRecords:add")
    @Log(title = "积分商城订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PointsMallRecordsBo bo) {
        return toAjax(pointsMallRecordsService.insertByBo(bo));
    }

    /**
     * 修改积分商城订单
     */
//    @SaCheckPermission("system:mallRecords:edit")
    @Log(title = "积分商城订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PointsMallRecordsBo bo) {
        return toAjax(pointsMallRecordsService.updateByBo(bo));
    }

    /**
     * 删除积分商城订单
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:mallRecords:remove")
    @Log(title = "积分商城订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(pointsMallRecordsService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 商城兑换
     */
    @PostMapping("/commodityExchange")
    @RepeatSubmit(interval = 8, timeUnit = TimeUnit.SECONDS, message = "点太快啦，稍后再试！")
    public R<Void> commodityExchange(@RequestBody @Validated CommodityExchangeBo bo) {
        return toAjax(pointsMallRecordsService.commodityExchange(bo));
    }

}
